package com.zhangbo.myapplication.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.zhangbo.myapplication.aidl.Book;
import com.zhangbo.myapplication.aidl.IBookManager;
import com.zhangbo.myapplication.aidl.IOnNewBookArrivedListener;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class BookManagerService extends Service {

    private static final String Tag = BookManagerService.class.getSimpleName();

    private CopyOnWriteArrayList<Book> books = new CopyOnWriteArrayList<>();

    private Binder binder = new IBookManager.Stub() {
        @Override
        public List<Book> getBookList() throws RemoteException {
            return books;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            books.add(book);
        }

        @Override
        public void registerListener(IOnNewBookArrivedListener listener) throws RemoteException {

        }

        @Override
        public void unregisterListener(IOnNewBookArrivedListener listener) throws RemoteException {

        }
    };


    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(Tag, "onCreate");
        books.add(new Book(1, "android"));
        books.add(new Book(2, "ios"));
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.e(Tag, "onBind");
        return binder;
    }

    @Override
    public void onDestroy() {
        Log.e(Tag, "onDestroy");
        super.onDestroy();
    }
}
